package com.yuyou.tas.util.tool;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * created by yuan
 */
public class ListUtil {

    /**
     * 深复制list
     * list实体类需要序列化
     * @param src
     * @param <T>
     * @return
     */
    public static <T> List<T> deepCopy(List<T> src) {
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);

            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            @SuppressWarnings("unchecked")
            List<T> dest = (List<T>) in.readObject();
            return dest;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.EMPTY_LIST;
    }
    
    
	public static <T,S> List<T> copyList(List<S> sourceList,Class<T> targetClazz) throws Exception, IllegalAccessException {
    	List<T> targetList=new ArrayList<T>();
    	for(S s:sourceList){
			T t = (T) targetClazz.newInstance();
    		DefindBeanUtils.copyProperties(s, t);
    		targetList.add(t);  
    	}  	
		return targetList;  	
    }
    
    

    
    
    
    
    
    
    
    
    
}
